package cn.sw.common.http;


import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @Description: 统一HTTP响应
 * @Author: 张众森
 * @Date: 2025/5/10
 */
public class HttpResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 响应码
     *
     * @mock 200
     */
    private String code;
    /**
     * 响应提示
     *
     * @mock success
     */
    private String message;
    /**
     * 请求ID
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String traceId;
    /**
     * 响应内容
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public HttpResponse() {
        // 设置响应码为200
        this.code = "200";
        // 设置响应消息为success
        this.message = "success";
    }

    /**
     * 返回一个成功的响应
     *
     * @param <T> 泛型
     * @return HttpResponse<T>
     */
    public static <T> HttpResponse<T> ok() {
        return baseCreate("200", "success", null);
    }

    /**
     * 返回一个成功的响应
     *
     * @param <T>     泛型
     * @param code    响应码
     * @param message 响应提示
     * @param data    响应内容
     * @return HttpResponse<T>
     */
    public static <T> HttpResponse<T> ok(String code, String message, T data) {
        return baseCreate(code, message, data);
    }

    /**
     * 返回一个成功的响应
     *
     * @param <T>     泛型
     * @param message 响应提示
     * @param data    响应内容
     * @return HttpResponse<T>
     */
    public static <T> HttpResponse<T> ok(String message, T data) {
        return baseCreate("200", message, data);
    }

    /**
     * 返回一个成功的响应
     *
     * @param <T>  泛型
     * @param data 响应内容
     * @return HttpResponse<T>
     */
    public static <T> HttpResponse<T> ok(T data) {
        return baseCreate("200", "success", data);
    }

    /**
     * 返回一个错误的响应
     *
     * @param <T> 泛型
     * @return HttpResponse<T>
     */
    public static <T> HttpResponse<T> error() {
        return error("500", "未知异常，请联系管理员");
    }

    /**
     * 返回一个错误的响应
     *
     * @param <T>     泛型
     * @param message 响应提示
     * @return HttpResponse<T>
     */
    public static <T> HttpResponse<T> error(String message) {
        return error("500", message);
    }

    /**
     * 返回一个错误的响应
     *
     * @param <T>  泛型
     * @param code 响应码
     * @param msg  响应提示
     * @return HttpResponse<T>
     */
    public static <T> HttpResponse<T> error(String code, String msg) {
        return baseCreate(code, msg, null);
    }

    /**
     * 返回一个错误的响应
     *
     * @param <T>  泛型
     * @param code 响应码
     * @param msg  响应提示
     * @param data 响应内容
     * @return HttpResponse<T>
     */
    public static <T> HttpResponse<T> error(String code, String msg, T data) {
        return baseCreate(code, msg, data);
    }

    /**
     * 创建一个响应
     *
     * @param <T>  泛型
     * @param code 响应码
     * @param msg  响应提示
     * @param data 响应内容
     * @return HttpResponse<T>
     */
    private static <T> HttpResponse<T> baseCreate(String code, String msg, T data) {
        HttpResponse result = new HttpResponse();
        result.setCode(code);
        result.setMessage(msg);
        // code scan open ,others close
//        String esApidata= JSONObject.toJSONString(data);
//        //HTML encode
//        esApidata=ESAPI.encoder().encodeForHTML(esApidata);
//        //HTML attribute encode
//        esApidata=ESAPI.encoder().encodeForHTMLAttribute(esApidata);
//        //JavaScript encode
//        esApidata=ESAPI.encoder().encodeForJavaScript(esApidata);
//        //CSS encode
//        esApidata= ESAPI.encoder().encodeForCSS(esApidata);
//        try{
//            //URL encode
//            esApidata=ESAPI.encoder().encodeForURL(esApidata);
//        }catch (Exception e){
//            e.getMessage();
//        }
//        result.setData(JSONObject.parseObject(esApidata,data.getClass()));
        result.setData(data);
        try {
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            result.setTraceId(String.valueOf(request.getAttribute("eipTraceId")));
        } catch (Exception e) {
            result.setTraceId(null);
        }
        return result;
    }

    /**
     * 返回一个错误的响应
     *
     * @param responseCode 响应码
     * @return HttpResponse<String>
     */
    public static HttpResponse<String> error(ResponseCode responseCode) {
        return baseCreate(responseCode.getCode(), responseCode.getMessage(), null);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
}
